package com.example.client;


import com.example.client.socket.GameWebSocketClient1;

import com.example.client.ui.GamePanel1;

import javax.swing.*;
import javax.swing.plaf.FontUIResource;
import java.awt.*;
import java.awt.event.*;
import java.net.URI;


public class GomokuGameClient extends JFrame implements MouseListener {
    private GamePanel1 gamePanel1;
    private GameWebSocketClient1 client;

    public static void main(String[] args) {
        setUIFont(new FontUIResource("微软雅黑", Font.PLAIN, 22));
        SwingUtilities.invokeLater(() -> {
            try {
                new GomokuGameClient();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });
    }

    public static void setUIFont(FontUIResource f) {     //是中文显示的方法
        java.util.Enumeration<Object> keys = UIManager.getDefaults().keys();
        while (keys.hasMoreElements()) {
            Object key = keys.nextElement();
            Object value = UIManager.get(key);
            if (value instanceof javax.swing.plaf.FontUIResource) {
                UIManager.put(key, f);
            }
        }
    }

    private JButton getJButton() {
        JButton ruleButton = new JButton("禁手规则");
        ruleButton.addActionListener(e -> {
                JOptionPane.showMessageDialog(this,
                        "禁手规则（仅对黑子有效）:\n" +
                                "1. 三三禁手：黑子一子落下同时形成两个或两个以上的活三\n" +
                                "2. 四四禁手：黑子一子落下同时形成两个或两个以上的四子\n" +
                                "3. 长连禁手：黑子一子落下形成六个或六个以上的连续棋子\n" +
                                "违反以上任何禁手规则，黑子理论上判负",
                        "禁手规则说明", JOptionPane.INFORMATION_MESSAGE);
                //JOptionPane.INFORMATION_MESSAGE是一种显示格式

        });
        return ruleButton;
    }

    public GomokuGameClient() throws Exception {
        setTitle("禁手五子棋？？！");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   //关闭
        setSize(675, 800);
        setLocationRelativeTo(null);      //居中
        setLayout(new BorderLayout());

        gamePanel1 = new GamePanel1(15,40,36,50);     //游戏面板
        add(gamePanel1, BorderLayout.CENTER);      //面板居中

        JPanel bottomPanel = new JPanel();  // 创建底部重开面板
        JButton resetButton = new JButton("重新开始");  // 创建重开按钮
        resetButton.addActionListener(e -> gamePanel1.initializeGame());//按钮效果
        bottomPanel.add(resetButton);

        JButton ruleButton = getJButton();
        bottomPanel.add(ruleButton);

        add(bottomPanel, BorderLayout.SOUTH);   //底部,中间会遮住游戏面板

        // 添加鼠标监听器
        addMouseListener(this);

        try {
            connectWebSocket();
        } catch (Exception e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this, "WebSocket连接失败: " + e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
        }
        // 连接WebSocket
        try {
            connectWebSocket();
        } catch (Exception e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this, "WebSocket连接失败: " + e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
        }

        setVisible(true);
    }

    private void connectWebSocket() throws Exception {
        client = new GameWebSocketClient1(new URI("ws://192.168.1.31:8080/game"),gamePanel1);
        gamePanel1.setWebSocketClient(client);
        client.connectBlocking();
    }


    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseClicked(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}